//
//  HomeTableViewController.swift
//  AlterationSwitch
//
//  Created by solarke_apple_mini_1 on 2018/10/25.
//  Copyright © 2018年 石家庄科林电气股份有限公司. All rights reserved.
//

import UIKit

class HomeViewController: BaseViewController,UITableViewDelegate,UITableViewDataSource{
    // MARK: - Properties
    var homeType = 1
    // MARK: - IBOutlet
    @IBOutlet weak var tableView:UITableView!
    @IBOutlet weak var flyView:UIView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.tableFooterView = UIView(frame: .zero)
        registerNotifications()
        IsWiFiContent()
    }
    
    func IsWiFiContent() {
        let name = UserDefaultsOrNothingValue(WiFiName, defaultValue: "")
        if getUsedSSID() == name {
            processClientSocket()
            flyView.backgroundColor = mainColor
        } else {
            flyView.backgroundColor = UIColor.lightGray
        }
    }
    
    func registerNotifications() {
        notificationCenter.addObserver(self, selector: #selector(sideStealType(sender:)), name: notificationSideStealType, object: nil)
    }
    
    @objc func sideStealType(sender: NSNotification) {
        let info = sender.userInfo as! [String: AnyObject]
        let type = info["homeType"] as! Int
        dealWithTurnHome(type: type)
    }
    
    func dealWithTurnHome(type:Int) {
        switch type {
        case canShuSheZhi:
            self.navigationItem.title = "参数设置"
        case chaxunKongZhi:
            self.navigationItem.title = "查询控制"
        case shuJu1Lei :
            self.navigationItem.title = "1类数据"
        case shuJu2Lei:
            self.navigationItem.title = "2类数据"
        default:
            break
        }
        homeType = type
        tableView.reloadData()
    }
    
    // MARK: - IBAction
    @IBAction func leftButtonPressed() {
        slideMenuController.switchController()
    }

    // MARK: - Table view data source
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        var num = 1
        switch homeType {
        case canShuSheZhi,chaxunKongZhi:
            num = 1
        case shuJu1Lei :
            num = 3
        case shuJu2Lei:
            num = 2
        default:
            break
        }
        return num
    }

    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        switch homeType {
        case canShuSheZhi:
            cell.textLabel?.text = "无线设置"
        case chaxunKongZhi:
            cell.textLabel?.text = "换相控制"
        case shuJu1Lei:
            let leiShuju = ["换相开关在线状态","电压不平衡度和电流不平衡度","换相开关数据"]
            cell.textLabel?.text = leiShuju[indexPath.row]
        case shuJu2Lei:
            cell.textLabel?.text = leiShuju[indexPath.row]
        default:
            break
        }
        cell.selectionStyle = .none
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        switch homeType {
        case canShuSheZhi:
            turnToView(name: "WIFISetting")
        case chaxunKongZhi:
            turnToView(name: "CommutationControl")
        case shuJu1Lei :
            switch indexPath.row {
                case 0:
                    turnToView(name: "CommutationSwitchOnlineStatus")
                case 1:
                    turnToView(name: "VoltageCurrentUnbalanceDegree")
                case 2:
                    turnToView(name: "CommutationSwitchData")
            default:break
            }
        case shuJu2Lei:
           let data: NSMutableDictionary = ["index":indexPath.row]
           self.navigationController?.pushViewControllerWithStoryboardID(id: "FrozenSwitchNumber", animated: true, data: data)
        default:
            break
        }
    }
    
    func turnToView(name:String) {
        self.navigationController?.pushViewControllerWithStoryboardID(id: name, animated: true, data: nil)
    }
}
